#include "gui/stockviewadddialog.h"
#include "gui/separatorwidget.h"
#include "core/constants.h"
#include "core/baseview.h"

void StockViewAddDialog::initDialogComponents() {
    BaseDialog::initDialogComponents();

    this->setMainText("Add a Stock");
    this->setSubText(QString("Enter your stock for %1 (%2)").arg(item->getName(), item->getCode()));

    gridLayout = new QGridLayout;

    serialLabel = new QLabel;
    serialEdit = new QLineEdit;

    serialLabel->setText("Serial Number:");

    gridLayout->addWidget(serialLabel, 0, 0);
    gridLayout->addWidget(serialEdit, 0, 1);

    mainLayout->addLayout(gridLayout);
    mainLayout->addStretch();

    mainLayout->addWidget(new SeparatorWidget);
    buttonBox = new QDialogButtonBox(QDialogButtonBox::Save | QDialogButtonBox::Cancel);
    mainLayout->addWidget(buttonBox);
}

void StockViewAddDialog::onSaveButtonClicked()
{
    if (this->serialEdit->text() == "") {
        QMessageBox::critical(this, "Incomplete data", "Please fill in all input boxes to continue.");
        return;
    }

    stockData = new StockModel;
    stockData->setItem(this->item);
    stockData->setSerial(this->serialEdit->text());
    this->accept();
}

void StockViewAddDialog::initDialogSignalsAndSlots()
{
    connect(this->buttonBox, SIGNAL(accepted()), this, SLOT(onSaveButtonClicked()));
    connect(this->buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
}

StockModel *StockViewAddDialog::getStock()
{
    return this->stockData;
}

StockViewAddDialog::StockViewAddDialog(QWidget *parent, ItemModel *item) :
    BaseDialog(parent)
{
    this->item = item;
    this->setWindowTitle("Add Stock");
    this->initDialogComponents();
    this->initDialogSignalsAndSlots();
}
